package wsi.exp;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.rygh.core.html.Html;
import org.rygh.core.html.element.HtmlOl;

import wsi.function.Function;
import wsi.ws.WebServices;

public abstract class Experiment {
	public interface Color {
		String red = "#FFBDE4";
		String blue = "#C4D6FF";
		String green = "#CDFFBD";
		String orange = "#FFC073";
	}

	protected void appendMissingHtml(Html html, Set<? extends Object> goldSet,
			Set<? extends Object> resultSet) {
		List<Object> missing_in_result = new ArrayList<>();
		List<Object> missing_in_gold = new ArrayList<>();

		for (Object function : goldSet) {
			if (!resultSet.contains(function))
				missing_in_result.add(function);
		}

		for (Object function : resultSet) {
			if (!goldSet.contains(function))
				missing_in_gold.add(function);
		}

		//

		HtmlOl ol;

		if (!missing_in_result.isEmpty()) {
			html.addParagraph("<b>Missing in Result</b>");
			ol = new HtmlOl();

			for (Object f : missing_in_result)
				ol.addElement(getName(f));

			html.addElement(ol);
		}

		//

		if (!missing_in_gold.isEmpty()) {
			html.addParagraph("<b>Missing in Goldset</b>");
			ol = new HtmlOl();

			for (Object f : missing_in_gold)
				ol.addElement(getName(f));

			html.addElement(ol);
		}
	}

	private String getName(Object o) {
		return (o instanceof Function) ? ((Function) o).getFullName()
				: (o instanceof WebServices) ? ((WebServices) o).name() : o
						.toString();
	}

	public Function parseFileName(String name) {
		String token = "_get";

		int pos = name.indexOf(token);
		assert pos != -1 : String.format("name [%s]", name);

		String service, func_name;

		service = name.substring(0, pos);
		func_name = name.substring(pos + 1);

		if (func_name.contains("_")) {
			pos = func_name.indexOf('_');
			func_name = func_name.substring(0, pos);
		}

		return new Function(service, func_name);
	}

	public abstract void compute();

	public abstract String save();
}
